package com.to.aboomy.bannersample.util;

import com.to.aboomy.bannersample.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * dialog
 *
 * @since 2021-07-21
 */
public class ArrayStringItemSelectDialog extends CommonDialog {
    private static final int NUMF1 = -1;
    private static final int NUM3000 = 3000;
    private static final int NUM500 = 500;
    private static final int NUM150 = 150;
    private OnItemClickListener onItemClickListener;
    private ArrayList<String> valueStrings;
    private Ability context;
    private int choose = NUMF1;
    private AnimatorProperty slideOut;
    private AnimatorProperty slideIn;
    private static int durationTime = NUM3000;
    private Component component;

    /**
     * 构造函数
     *
     * @param context context
     */
    public ArrayStringItemSelectDialog(Ability context) {
        super(context);
        this.context = context;
        initViewDialog();
    }

    /**
     * ArrayStringItemSelectDialog
     *
     * @param ability ability
     * @param asList asList
     */
    public ArrayStringItemSelectDialog(Ability ability, List<String> asList) {
        super(ability);
        this.context = ability;
        this.valueStrings = new ArrayList(asList);
        initViewDialog();
    }

    /**
     * 初始化
     */
    private void initViewDialog() {
         component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_info_dialog, null, true);
        initComponent(component);
        ArrayStringItemSelectDialog.this.setAutoClosable(true);
        ArrayStringItemSelectDialog.this.setMovable(false);
        ArrayStringItemSelectDialog.this.setTransparent(true);
        ArrayStringItemSelectDialog.this.setContentCustomComponent(component);
        ArrayStringItemSelectDialog.this.setAlignment(LayoutAlignment.BOTTOM);

    }

    private void initComponent(Component component) {
        Text text = (Text) component.findComponentById(ResourceTable.Id_tv_bottom_select_cancel);
        text.setMarginBottom(NUM150);
        text.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component1) {
                exitAnimation(component);
                ArrayStringItemSelectDialog.this.remove();
            }
        });
        ListContainer listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_rv_bottom_select);
        MyAdapter adapter = new MyAdapter(valueStrings);
        listContainer.setItemProvider(adapter);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long l) {
                choose = position;
                onItemClickListener.onItemClick(position,valueStrings.get(position));
                ArrayStringItemSelectDialog.this.remove();
            }
        });
    }

    public ArrayStringItemSelectDialog setValueStrings(List<String> valueStrings) {
        this.valueStrings = new ArrayList<>(valueStrings);
        return this;
    }

    public interface OnItemClickListener {
        /**
         * onItemClick
         *
         * @param position position
         * @param value value
         */
        void onItemClick(int position, String value);
    }

    public ArrayStringItemSelectDialog setChoose(int choose){
        this.choose = choose;
        return this;
    }


    public ArrayStringItemSelectDialog setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    private class MyAdapter extends BaseItemProvider {

        private ArrayList<String> valueStrings;

        /**
         * 构造函数
         *
         * @param valueStrings valueStrings
         */
        public MyAdapter(ArrayList<String> valueStrings) {
            super();
            this.valueStrings = valueStrings;
        }

        @Override
        public int getCount() {
            return valueStrings.size();
        }

        @Override
        public Object getItem(int position) {
            return valueStrings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
            final Component cpt;
            if (component == null) {
                cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_sample, null, false);
            } else {
                cpt = component;
            }
            Text text = (Text) cpt.findComponentById(ResourceTable.Id_tv_bottom_select_content);
            text.setText(valueStrings.get(position));
            text.setTextColor(choose == position ? Color.RED : new Color(Color.getIntColor("#333333")));
            return cpt;
        }
    }

    private void exitAnimation(final Component exitView) {
        if (exitView == null) {
            return;
        }
        if (slideOut == null) {
            slideOut = new AnimatorProperty(exitView);
            slideOut.moveFromY(0).moveToY(NUM500).setDuration(durationTime);
        } else {
            slideOut.setTarget(exitView);
        }
    }

    private void enterAnimation(Component enterView) {
        if (enterView == null) {
            return;
        }
        if (slideIn == null) {
            slideIn = new AnimatorProperty(enterView);
            slideIn.moveFromY(NUM500).moveToY(0).setDuration(durationTime);
        } else {
            slideIn.setTarget(enterView);
        }
        slideIn.start();
    }

    @Override
    protected void onShow() {
        super.onShow();
        enterAnimation(component);
    }
}
